package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class SpuManageController {

    @Autowired
    private ManageService manageService;

    //保存spu
    ///admin/product/saveSpuInfo
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        manageService.save(spuInfo);
        return Result.ok();
    }

    ////admin/product/baseSaleAttrList
    //根据spuid获取销售属性
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrInfoList = manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrInfoList);
    }

    //admin/product/getSpuInfo/5
    //根据spuid获取spuInfo
    @GetMapping("getSpuInfo/{spuId}")
    public Result getSpuInfo(@PathVariable Long spuId){
        SpuInfo spuInfo = manageService.getSpuInfo(spuId);
        return Result.ok(spuInfo);
    }

    //admin/product/updateSpuInfo
    //修改spuInfo
    @PostMapping("updateSpuInfo")
    public Result updateSpuInfo(@RequestBody SpuInfo spuInfo){
        manageService.updateSpuInfo(spuInfo);
        return Result.ok();
    }

    //根据spuId 查询spuImageList
    ///admin/product/spuImageList/{spuId}
    @GetMapping("spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId){
        List<SpuImage> spuImageList = manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    ///admin/product/spuSaleAttrList/{spuId}
    //根据spuId 查询销售属性
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
}
